package virtorina.client.room;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

import virtorina.client.room.dto.UserDTO;
import virtorina.client.room.rpc.GWTService4Room;
import virtorina.client.room.rpc.GWTService4RoomAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Text;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class Users {
	private VerticalPanel main = new VerticalPanel();
	private final GWTService4RoomAsync usergetter = GWT
			.create(GWTService4Room.class);
	private HTML Status = new HTML();
	private UserDTO user;
	private ArrayList<UserDTO> allUsers;
	private Contacts users;
	private Integer ROOM_ID;
	private Virtorina virt;

	public Users(int id, Virtorina virt) {
		this.virt = virt;
		ROOM_ID = id;
		final ScrollPanel ScrlPnl = new ScrollPanel();
		allUsers = new ArrayList<UserDTO>();
		ScrlPnl.setHeight("492px");
		users = new Contacts();
		ScrlPnl.add(users);
		main.add(ScrlPnl);
		Status.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		Status.setHTML("...");
		RootPanel.get("number").add(Status);
	}

	public void setUser(UserDTO user) {
		chekNewUsers();
		this.user = user;
	}

	private void updateN() {
		Status.setHTML(allUsers.size()
				+ " <img src='imgs/people.png' width='20' height='20' title='в игре' alt='(в игре)' />");
	}

	void chekNewUsers() {
		usergetter.getNewUsers(ROOM_ID, new AsyncCallback<LinkedList<UserDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
				System.out.println("Fail on get users");

			}

			@Override
			public void onSuccess(LinkedList<UserDTO> result) {
				int k;
				a: for (UserDTO u : result) {
					if (u.getName().equals("null")) {
						continue;
					}
					k = 0;
					for (UserDTO au : allUsers) {
						if (u.equals(au)) {
							continue a;
						} else if (u.compareTo(au) > 0) {
							break;
						}
						k++;
					}
					allUsers.add(k, u);
					ContactEntity t = new ContactEntity(u.getID(), u.getName());
					users.add(t, k);
				}
				updateN();
			}

		});
	}

	VerticalPanel getUsers() {
		return main;
	}

	public void checkLeaveUsers() {
		usergetter.getLeaveUsers(ROOM_ID, new AsyncCallback<LinkedList<Long>>() {

			@Override
			public void onFailure(Throwable caught) {
				System.out.println("Fail on get leaver users");

			}

			@Override
			public void onSuccess(LinkedList<Long> result) {
				for (Long id : result) {
					for (UserDTO u : allUsers) {
						if (u.getID().equals(id)) {
							allUsers.remove(u);
							users.remove(u.getID());
						}
					}
				}
				updateN();
			}

		});

	}

	class ContactEntity extends VerticalPanel {
		HTML viewName;
		Image status;
		HTML stat;
		String name;
		Long id;
		boolean collapse;
		int shift;

		// int state;

		Timer shifter = new Timer() {

			@Override
			public void run() {
				String s = getName();
				if (s.length() <= 11) {
					return;
				} else if (s.length() - shift < 9) {
					shift = 0;
					return;
				} else {
					s = s.substring(shift, shift + 9);
					viewName.setHTML(getShortInfo(s));
					shift++;
					return;
				}
			}

		};

		ContactEntity(Long id, String name) {
			this.id = id;
			this.collapse = true;
			this.name = name;
			status = new Image("imgs/circle_green.png");
			status.setAltText("(Active)");
			status.setTitle("(Active)");
			stat = new HTML();
			stat.setHTML("Quick Stat");
			stat.setVisible(false);
			viewName = new HTML();
			viewName.setHTML(getShortInfo(getName()));
			HorizontalPanel top = new HorizontalPanel();
			top.add(status);
			top.add(viewName);
			this.add(top);
			this.add(stat);

			viewName.addMouseMoveHandler(new MouseMoveHandler() {

				@Override
				public void onMouseMove(MouseMoveEvent event) {
					shifter.scheduleRepeating(200);
				}
			});
			viewName.addMouseOutHandler(new MouseOutHandler() {

				@Override
				public void onMouseOut(MouseOutEvent event) {
					shift = 0;
					shifter.cancel();
					viewName.setHTML(getShortInfo(getName()));

				}
			});
			viewName.addMouseDownHandler(new MouseDownHandler() {

				@Override
				public void onMouseDown(MouseDownEvent event) {
					System.out.println("click");
					if (collapse) {
						expand();
					} else {
						collapse();
					}

				}
			});
		}

		String getName() {
			return name;
		}

		String getShortInfo(String nick) {
			StringBuilder name = new StringBuilder(nick);
			if (name.length() > 9) {
				name = new StringBuilder(name.subSequence(0, 9));
				name.append("...");
			}
			String h = new String("<span title='" + getName() + "'>"
					+ name.toString() + "</span><br>");
			return h;
		}

		void expand() {
			stat.setVisible(true);
			collapse = false;
		}

		void collapse() {
			stat.setVisible(false);
			collapse = true;
		}
	}

	class Contacts extends VerticalPanel {

		public void add(ContactEntity e, int k) {
			super.insert(e, k);
		}

		public void remove(Long id) {
			Iterator<Widget> i = super.iterator();
			int k = 0;
			while (i.hasNext()) {
				if (((ContactEntity) i.next()).id.equals(id)) {
					super.remove(k);
				}
				k++;
			}
		}
	}
}
